package com.nlp.visualization.core.format;


import com.nlp.visualization.common.CONSTANTS;
import com.nlp.visualization.service.tasks.WeiboJsonPreProcessTask;
import com.nlp.visualization.service.tasks.WeiboSegTask;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.TimeUnit;

public class JsonUtil {
    private static Logger logger = LoggerFactory.getLogger(JsonUtil.class);

    /**
     * 老师给出的数据并不是标准的json格式，需要对其进行预处理。
     * {
     * "_id" : ObjectId("5a3a65eef8e7eccbc7d73d16"),
     * "is_message" : "yes",
     * "user_id" : "6122911090",
     * "weibo_id" : "Fpfwp8aTN",
     * "weibo_info" : {
     * "comment_count" : "0",
     * "weibo_content" : "转发了 For_BrAnT_B个人站 的微博:#红花会BrAnTB小白#   很酷 但也是个不舍得与粉丝告别的小男孩@红花会_BrAnTB小白#waiting-红花会_Official[音乐]#  【很酷，不接受批评】 ​​​ 原图 赞[365] 原文转发[113] 原文评论[145]转发理由:#红花会BrAnTB小白# 我真的见不得他有一点委屈吧，嘴角一下咧，我就想把全世界最甜的糖给他[失望]@红花会_BrAnTB小白   赞[8] 转发[0] 评论[0] 收藏 10月07日 12:26 来自iPhone 7 Plus",
     * "original_reprint_count" : "113",
     * "reprint_count" : "0",
     * "weibo_id" : "Fpfwp8aTN",
     * "reprint_reason" : "#红花会BrAnTB小白# 我真的见不得他有一点委屈吧，嘴角一下咧，我就想把全世界最甜的糖给他[失望]@红花会_BrAnTB小白   ",
     * "publish_time" : "10月07日 12:26 来自iPhone 7 Plus",
     * "weibo_from" : "iPhone 7 Plus",
     * "praise_count" : "8",
     * "is_reprint_weibo" : true,
     * "original_praise_count" : "365",
     * "original_comment_count" : "145",
     * "weibo_content_url" : "https://weibo.cn/comment/Fpfwp8aTN?rl=0#cmtfrm"
     * }
     * },
     *
     * @param weiboJsonFilePath
     */
    public static void weiboJsonDataPreProcess(String weiboJsonFilePath, String weiboJsonOutputPath) {

        long start = System.currentTimeMillis();
        List<String> list = new ArrayList<>();
        //按行读取文件
        try {
            File jsonFile = new File(weiboJsonFilePath);
            list = FileUtils.readLines(jsonFile, "UTF-8");
            // 创建线程池
            ForkJoinPool forkJoinPool = new ForkJoinPool();
            // 提交替换的任务
            forkJoinPool.submit(new WeiboJsonPreProcessTask(0, list.size(), list));
            forkJoinPool.shutdown();
            forkJoinPool.awaitTermination(CONSTANTS.TASK_TIMEOUT, TimeUnit.SECONDS);//阻塞当前线程直到 ForkJoinPool 中所有的任务都执行结束
            // 替换成jsonArray的格式
            list.set(0, list.get(0).replace("{", "[{"));
            list.set(list.size() - 1, list.get(list.size() - 1).replace(",", "]"));
            //写回文件
            File file = new File(weiboJsonOutputPath);
            FileUtils.writeLines(file, list, false);
        } catch (IOException e) {
            logger.error("预处理微博json文件异常");
            e.printStackTrace();
        } catch (Exception e) {
            logger.error("预处理微博json文件未知异常");
            e.printStackTrace();
        }

    }
}
